from selenium import webdriver

# driver = webdriver.Chrome(executable_path=r"D:\PythonProject\爬虫\bs\chromedriver.exe")
def get_province():
    driver = webdriver.Chrome(executable_path=r"D:\PythonProject\爬虫\bs\chromedriver.exe")
    driver.get('https://xingzhengquhua.51240.com/')
    table = driver.find_element_by_xpath('//table')
    tds = table.find_elements_by_xpath('.//td/a')
    province = {}
    for td in tds:
        if '全国' in td.text or td.text in td.get_attribute('href'):
            continue
        province[td.text] = td.get_attribute('href')
    driver.close()
    return province


def get_city():
    driver = webdriver.Chrome(executable_path=r"D:\PythonProject\爬虫\bs\chromedriver.exe")
    for province_name,province_url in get_province().items():
        driver.get(province_url)
        table = driver.find_element_by_xpath('//table')
        tds = table.find_elements_by_xpath('.//td/a')
        city = {}
        for td in tds:
            if '全国' in td.text or td.text in td.get_attribute('href'):
                continue
            city[td.text] = td.get_attribute('href')
            print(province_name, td.text)
    driver.close()


get_city()